import requests
import time
import execjs
from hashlib import md5

url = 'https://mhapi.yiche.com/hcar/h_car/api/v1/param/get_param_details'
timestamp = str(int(time.time() * 1000))
e = {
    "url": "https://mhapi.yiche.com/hcar/h_car/api/v1/param/get_param_details",
    "data": {
        "cityId": "3101",
        "serialId": "10628"
    },
    "headers": {
        "x-platform": "pc"
    },
    "method": "GET",
    "withCredentials": "true",
    "async": "true",
    "isParam": "true",
    "dataType": "json",
    "defaultContentType": "true",
    "encryptType": "headers",
    "isEncrypt": "false",
    "isBrush": "false",
    "proxy": "false",
    "timeout": 5000
};
t = {
    "cid": "508",
    "ver": "v11.35.0",
    "timestamp": timestamp,
    "gradeParam": {},
    "uid": "",
    "headerEncryptKeys": [
        {
            "name": "pc",
            "value": "19DDD1FBDFF065D3A4DA777D2D7A81EC",
            "cid": "508"
        },
        {
            "name": "phone",
            "value": "DB2560A6EBC65F37A0484295CD4EDD25",
            "cid": "601"
        },
        {
            "name": "h5",
            "value": "745DFB2027E8418384A1F2EF1B54C9F5",
            "cid": "601"
        },
        {
            "name": "business_applet",
            "value": "64A1071F6C3C3CC68DABBF5A90669C0A",
            "cid": "601"
        },
        {
            "name": "wechat",
            "value": "AF23B0A6EBC65F37A0484395CE4EDD2K",
            "cid": "601"
        },
        {
            "name": "tencent",
            "value": "1615A9BDB0374D16AE9EBB3BBEE5353C",
            "cid": "750"
        }
    ],
    "paramsKey": "f48aa2d0-31e0-42a6-a7a0-64ba148262f0"
};
with open(r'E:\Python\vscode\Crawlers\js逆向\05易车参数\sign_a.js', 'r', encoding='utf-8') as f:
    js = execjs.compile(f.read())

decode_string = js.call('s',e,t)
md5_obj = md5()
md5_obj.update(decode_string.encode(encoding='utf-8'))
x_sign = md5_obj.hexdigest()
headers = {
    'user-agent' : 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36',
    "cid": "508",
    "content-type": "application/json;charset=UTF-8",
    "reqid": "e1808970b474f7568fb48c84af09ffbe",
    "x-city-id": "3101",
    "x-ip-address": "211.158.243.134",
    "x-platform": "pc",
    "x-sign": x_sign,
    "x-timestamp": timestamp,
    "x-user-guid": "b6df73ee-efd2-4a95-ae8d-683ac596c170"
    }


params = {
    "cid": "508",
    "param": "{\"cityId\":\"3101\",\"serialId\":\"10628\"}"
}

resp = requests.get(url, headers=headers, params=params)
json_data = resp.json()
json_data = json_data['data']['list']
for i in json_data:
    print(i['name'])
